package com.bluefish.bltuan.interceptor;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 
 *
 */
public class PerformanceAnalysisInterceptor implements Interceptor {

	 

	private static final long serialVersionUID = 1L;

	private final Logger log = LoggerFactory.getLogger(PerformanceAnalysisInterceptor.class);
	
	public void destroy() {
	}

	public void init() {
	}


	public String intercept(ActionInvocation ai) throws Exception {
		long start = System.currentTimeMillis();
		long threadID = Thread.currentThread().getId();
		
		String requestMethod = ServletActionContext.getRequest().getMethod();
		
		String method = ServletActionContext.getRequest().getParameter("_method");
		
		requestMethod = method == null ? requestMethod : method;
		
		String requestPath = ServletActionContext.getRequest().getServletPath();
		log.info("\t[Thread {}] receive {} request \"{}\"",
				new Object[]{+threadID, requestMethod, requestPath});
		
		String result = ai.invoke();
		
		long duration = System.currentTimeMillis() - start;
		log.info("\t[Thread {}] complete {} request \"{}\" with total time {} ms\n",
				new Object[]{+threadID, requestMethod, requestPath, duration});
		return result;
	}

}
